package dfs;



import java.util.LinkedList;
import java.util.List;

public class 电话号码的字母组合 {

    List<String> lists = new LinkedList<>();
    public List<String> letterCombinations(String digits) {
        if (digits.length()==0 || digits==null)
            return lists;
        StringBuilder builder = new StringBuilder();
        dfs(digits,builder,0);
        return lists;
    }

    private void dfs(String digits,StringBuilder builder ,int s) {
        if (builder.length()==digits.length()){
            lists.add(builder.toString());
            return;
        }
        char[] chars = digits.toCharArray();
        char[] method = method(chars[s]);
        for (int j = 0; j < method.length; j++) {
            builder.append(method[j]);
            dfs(digits,builder,s+1);
            builder.deleteCharAt(builder.length()-1);
        }
    }
    private char[] method(char digit) {
        if (digit == '2') {
            return new char[] {'a', 'b', 'c'};
        } else if (digit == '3') {
            return new char[] {'d', 'e', 'f'};
        } else if (digit == '4') {
            return new char[] {'g', 'h', 'i'};
        } else if (digit == '5') {
            return new char[] {'j', 'k', 'l'};
        } else if (digit == '6') {
            return new char[] {'m', 'n', 'o'};
        } else if (digit == '7') {
            return new char[] {'p', 'q', 'r', 's'};
        } else if (digit == '8') {
            return new char[] {'t', 'u', 'v'};
        } else {
            return new char[] {'w', 'x', 'y', 'z'};
        }
    }
}
